package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.HealthNormalRange;
import com.health_predict_springboot.service.HealthNormalRangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/normal-range")
public class HealthNormalRangeController {

    @Autowired
    private HealthNormalRangeService healthNormalRangeService;

    @GetMapping("/{id}")
    public HealthNormalRange getNormalRangeById(@PathVariable("id") Integer id) {
        return healthNormalRangeService.getNormalRangeById(id);
    }

    @GetMapping("/indicator/{name}")
    public List<HealthNormalRange> getNormalRangeByIndicatorName(@PathVariable("name") String indicatorName) {
        return healthNormalRangeService.getNormalRangeByIndicatorName(indicatorName);
    }

    @GetMapping("/all")
    public List<HealthNormalRange> getAllNormalRanges() {
        return healthNormalRangeService.getAllNormalRanges();
    }

    @PostMapping
    public boolean addNormalRange(@RequestBody HealthNormalRange normalRange) {
        normalRange.setUpdateTime(new Date());
        return healthNormalRangeService.addNormalRange(normalRange);
    }

    @PutMapping
    public boolean updateNormalRange(@RequestBody HealthNormalRange normalRange) {
        normalRange.setUpdateTime(new Date());
        return healthNormalRangeService.updateNormalRange(normalRange);
    }

    @DeleteMapping("/{id}")
    public boolean deleteNormalRange(@PathVariable("id") Integer id) {
        return healthNormalRangeService.deleteNormalRange(id);
    }

    @GetMapping("/page")
    public List<HealthNormalRange> getNormalRangesByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String indicatorName) {
        Map<String, Object> params = Map.of(
                "indicatorName", indicatorName,
                "offset", (pageNum - 1) * pageSize,
                "pageSize", pageSize
        );
        return healthNormalRangeService.getNormalRangesByPage(params);
    }

    @GetMapping("/count")
    public int getNormalRangeCount(
            @RequestParam(required = false) String indicatorName) {
        Map<String, Object> params = Map.of(
                "indicatorName", indicatorName
        );
        return healthNormalRangeService.getNormalRangeCount(params);
    }
}